package com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.globals;

import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSEnvironment;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSInterp;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.global.BaseJSConstants;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.global.BaseJSNativeFunction;

public class BaseJSEvalFunction extends BaseJSNativeFunction implements BaseJSConstants {

	public BaseJSEvalFunction() {
		super("eval", new String[]
			{ "a" });
	}

	@Override
	public Object invoked(BaseJSEnvironment e, BaseJSNativeFunction f, Object... variables) {
		if (variables == null)
			throw new NullPointerException("variables == null!");
		if (variables.length == 0)
			return null;
		final BaseJSEnvironment parsed = BaseJSInterp.get().interpret((String) variables[0]);
		if (parsed.hasReturnValue())
			return parsed.getReturnValue();
		return UNDEFINED;
	}

}
